package com.lc.utils;
import java.util.Arrays;
import java.util.Set;

import com.lc.service.*;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.AssignableTypeFilter;
/**
 * Created by lichao on 2017/6/1.
 */
    /**
     *
     * <p class="detail">
     * 功能：自定义类扫描
     * </p>
     * @ClassName: KingClassPathMapperScanner
     * @version V1.0
     * @date 2014年12月17日
     * @author <a href="mailto:wangs@financegt.com">kings</a>
     */
    public class KingClassPathMapperScanner extends ClassPathBeanDefinitionScanner  {

        public KingClassPathMapperScanner(BeanDefinitionRegistry registry,Class<?> kInterface) {
            super(registry);
            //根据注解扫描出需要实例化的类
            addIncludeFilter(new AnnotationTypeFilter(com.lc.service.MyResource.class));
            //根据实现的接口扫描出需要实例化的类
            //addIncludeFilter(new AssignableTypeFilter(KInterface));
        }

        @Override
        public Set<BeanDefinitionHolder> doScan(String... basePackages) {
            Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);

            if (beanDefinitions.isEmpty()) {
                logger.warn("No IRequest mapper was found in '" + Arrays.toString(basePackages)+ "' package. Please check your configuration.");
            }
            GenericBeanDefinition definition;
            System.out.println("init");
            for (BeanDefinitionHolder holder : beanDefinitions) {
                //把代理类放进去
                definition = (GenericBeanDefinition) holder.getBeanDefinition();
                    definition.getPropertyValues().add("proxy",
                    getRegistry().getBeanDefinition("kingProxy"));
                //把接口的类型设置进去
                definition.getPropertyValues().add("kInterface", definition.getBeanClassName());
                //设置Bean的真实类型MapperFactoryBean
                definition.setBeanClass(KingBeanFactory.class);
            }

            return beanDefinitions;
        }

        /**
         * 默认不允许接口的,这里重写,覆盖下
         */
        @Override
        protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
            return beanDefinition.getMetadata().isInterface()
                    && beanDefinition.getMetadata().isIndependent();
        }
    }
